/*
 * Chocolate Tunnel v0.6 Beta
 */
package Chocolate;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioSystem;
import javax.swing.JOptionPane;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

import console.main.ProxyRunner;
import network.httpProxy.HTTPProxyServerStarter;
import network.httpsServer.HttpsServerStarter_443;
import network.httpsServer.HttpsServerStarter_8443;
import network.sslProxy.SSLProxyStarter;
import network.utils.ProxyConstants;
import com.sun.org.apache.bcel.internal.generic.BREAKPOINT;


/**
 *
 * @author Achmat Toffa
 * @email: Achmat.Toffa@gmail.com
 * 
 */
public class MainUI extends javax.swing.JFrame {
    
    static int colourLooper, x, y; //X and Y co-ordinates
    static String whichButton, secretCode; //Secret code to init the program

    
    AudioInputStream startTuneInputStream;
    Clip startTuneClip;

    static long transferBytes = 0; // Init transfer ammount
        
    //Copy proxy certificate to folder that chocolate is situated in
    public void copyFile(final MainUI proxyUIStarter, final InputStream in) {
	
        final String currentDir = new File("").getAbsolutePath();
        try {
            final File f2 = new File(currentDir + File.separator + "ProxyCertificate.ser");
            final OutputStream out = new FileOutputStream(f2);
            final byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
	}   
        catch (final Exception ex) {
            JOptionPane.showMessageDialog(proxyUIStarter, ex
			+ "Could not create SSL Certificate file in  : "
			+ currentDir + File.separator + "ProxyCertificate.ser",
			"Message", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
	}           
    }     
    
    //Button colour changer
    public static class Looper extends Thread{
        public  void run(){
            (new ButtonChange()).start();
        }
    }
    
    public static class ButtonChange extends Thread{
        public void run(){
        
        
                
            while (colourLooper != 2) {            
        
        
                try {
                    Thread.sleep(50); //Change the colour every 50 milliseconds
                    
                    //Checks which button is whichButton and assigns a random colour to it
                    if (whichButton == jbStart.getText() && colourLooper == 1 ) {
                        jbStart.setBackground(new java.awt.Color((int)(Math.random() * 255),
                        (int)(Math.random() * 255), (int)(Math.random() * 255)));
                    }
          /*        if (whichButton == jbStop.getText() && colourLooper == 1) {
                        jbStop.setBackground(new java.awt.Color((int)(Math.random() * 255),
                        (int)(Math.random() * 255), (int)(Math.random() * 255)));
                    }*/
                    if (whichButton == jbAbout.getText() && colourLooper == 1) {
                        jbAbout.setBackground(new java.awt.Color((int)(Math.random() * 255),
                        (int)(Math.random() * 255), (int)(Math.random() * 255)));
                    }
                    if (whichButton == jbSettings.getText() && colourLooper == 1) {
                        jbSettings.setBackground(new java.awt.Color((int)(Math.random() * 255),
                        (int)(Math.random() * 255), (int)(Math.random() * 255)));
                    }
                    if (whichButton == jbExit.getText() && colourLooper == 1) {
                        jbExit.setBackground(new java.awt.Color((int)(Math.random() * 255),
                        (int)(Math.random() * 255), (int)(Math.random() * 255)));
                    }
            
                    if (whichButton == Settings.jbCancel.getText() && colourLooper == 1) {
                        Settings.jbCancel.setBackground(new java.awt.Color((int)(Math.random() * 255),
                        (int)(Math.random() * 255), (int)(Math.random() * 255)));
                    }
                    if (whichButton == Settings.jbSave.getText() && colourLooper == 1) {
                        Settings.jbSave.setBackground(new java.awt.Color((int)(Math.random() * 255),
                        (int)(Math.random() * 255), (int)(Math.random() * 255)));
                    }
                    if (whichButton == About.jbAboutBack.getText() && colourLooper == 1) {
                        About.jbAboutBack.setBackground(new java.awt.Color((int)(Math.random() * 255),
                        (int)(Math.random() * 255), (int)(Math.random() * 255)));
                    }
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
                }       
            } 
    
        }
    }
    //Data transfer counter and skull changer
    public class TransferCount extends Thread{
        public void run(){
            (new SkullChange()).start();
        }
    }
    
    /*Program startup thread
    * once the correct code ("justine") is entered
    * run a time counter and do the following
    */
    public class RunCount extends Thread {
        public void run() {

        
            while (startTuneClip.isActive() == true) { //Do while audio clip is playing
                try {            
                    sleep(6250);   
                    mainPanel.setEnabled(true);
                    mainPanel.setVisible(true);
                    startPanel.setVisible(false);
                    startPanel.setEnabled(false);
                    setTitle("Chocolate");
                    
                    //Exit the loop but continue to play the audio clip
                    break;  
                }
                catch (InterruptedException ex) {
                    Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    /**
     * Creates new form MainUI
     */
    public MainUI() {

        initComponents();
        
        try{
            startTuneInputStream = AudioSystem.getAudioInputStream(getClass()
            .getResource("/Chocolate/intro.wav"));
            startTuneClip = AudioSystem.getClip();
            startTuneClip.open(startTuneInputStream);
        }
        catch(Exception e){System.out.println("Failure to load audio clip");
        }
    }     
    
    /*
     * Skullchange thread to indicate that the proxy is working
     * and how many data is being used
     */
     public class SkullChange extends Thread {
        public void run(){
            while (jbStart.isEnabled()== false){
                try {
                    sleep(1500);
                    transferBytes = ProxyConstants.TOTAL_TRANSFER;
                
                    if (transferBytes > 0){
                        jLSkullRed.setVisible(false);
                        jLSkullGreen.setVisible(true);
                
                        if (transferBytes >= 1) {
                            setTitle("Chocolate Stole " +
                            (transferBytes) + " B!");
                        }
                    
                        if (transferBytes/1024 >= 1 && transferBytes/1024 <= 1024) {
                            setTitle("Chocolate Stole " +
                            (transferBytes/1024) + " KB!");    
                        }
                    
                        if (transferBytes/1024/1024 >= 1 && transferBytes/1024/1024 <= 1024) {
                            int kByteI = (int)transferBytes/1024;
                            double Mb = (double)kByteI/1024;
                            double Mbd = (int)(Mb*100);
                            Mbd = Mbd/100;
                            setTitle("Chocolate Stole " + Mbd + " MB!"); 
                        }
            
                        if (transferBytes/1024/1024/1024 >= 1) {
                            int kByteI = (int)transferBytes/1024;
                            double Mb = (double)kByteI/1024;
                            double Mbd = (int)(Mb*100);
                            Mbd = Mbd/100;
                            double Gb = Mbd/1024;
                            double Gbd = (int)(Gb*100);
                            Gbd = Gbd/100; 
                            setTitle("Chocolate Stole " + Gbd + " GB!");                    
                        }
                    }
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (jbStart.isEnabled()== true && transferBytes > 0 ){
                jLSkullGreen.setVisible(false);
                jLSkullRed.setVisible(true);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        startPanel = new javax.swing.JPanel();
        startPanelLabel1 = new javax.swing.JLabel();
        mainPanel = new javax.swing.JPanel();
        mainPanel.setEnabled(false);
        mainPanel.setVisible(false);
        mainMenuPanel = new javax.swing.JPanel();
        jbStart = new javax.swing.JButton();
        jbStop = new javax.swing.JButton();
        jbStop.setVisible(false);
        jbSettings = new javax.swing.JButton();
        jbAbout = new javax.swing.JButton();
        jbExit = new javax.swing.JButton();
        jbFormVisible = new javax.swing.JButton();
        jLSkullRed = new javax.swing.JLabel();
        jLSkullGreen = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chocolate");
        setMaximumSize(new java.awt.Dimension(300, 350));
        setMinimumSize(new java.awt.Dimension(300, 350));
        setName("startFrame"); // NOI18N
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
        });
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }
        });

        startPanel.setBackground(new java.awt.Color(254, 254, 254));
        startPanel.setMaximumSize(new java.awt.Dimension(300, 350));
        startPanel.setMinimumSize(new java.awt.Dimension(300, 350));
        startPanel.setName(""); // NOI18N
        startPanel.setPreferredSize(new java.awt.Dimension(300, 350));
        startPanel.setLayout(new javax.swing.BoxLayout(startPanel, javax.swing.BoxLayout.LINE_AXIS));

        startPanelLabel1.setBackground(new java.awt.Color(254, 254, 254));
        startPanelLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Chocolate/Anonymous.jpg"))); // NOI18N
        startPanelLabel1.setFocusable(false);
        startPanelLabel1.setInheritsPopupMenu(false);
        startPanelLabel1.setRequestFocusEnabled(false);
        startPanelLabel1.setVerifyInputWhenFocusTarget(false);
        startPanel.add(startPanelLabel1);

        mainPanel.setMaximumSize(new java.awt.Dimension(300, 350));
        mainPanel.setMinimumSize(new java.awt.Dimension(300, 350));
        mainPanel.setPreferredSize(new java.awt.Dimension(300, 350));
        mainPanel.setLayout(new java.awt.GridBagLayout());

        mainMenuPanel.setFocusable(false);
        mainMenuPanel.setMaximumSize(new java.awt.Dimension(300, 350));
        mainMenuPanel.setMinimumSize(new java.awt.Dimension(300, 350));
        mainMenuPanel.setOpaque(false);

        jbStart.setBackground(new java.awt.Color(44, 44, 44));
        jbStart.setForeground(new java.awt.Color(254, 254, 254));
        jbStart.setText("Start");
        jbStart.setToolTipText("Starts the tunnel");
        jbStart.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbStart.setFocusPainted(false);
        jbStart.setFocusable(false);
        jbStart.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jbStartMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jbStartMouseEntered(evt);
            }
        });
        jbStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStartActionPerformed(evt);
            }
        });

        jbStop.setBackground(new java.awt.Color(44, 44, 44));
        jbStop.setForeground(new java.awt.Color(254, 254, 254));
        jbStop.setText("Stop");
        jbStop.setToolTipText("Ends the tunnel");
        jbStop.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbStop.setEnabled(false);
        jbStop.setFocusPainted(false);
        jbStop.setFocusable(false);
        jbStop.setRequestFocusEnabled(false);
        jbStop.setVerifyInputWhenFocusTarget(false);
        jbStop.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jbStopMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jbStopMouseEntered(evt);
            }
        });
        jbStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbStopActionPerformed(evt);
            }
        });

        jbSettings.setBackground(new java.awt.Color(44, 44, 44));
        jbSettings.setForeground(new java.awt.Color(254, 254, 254));
        jbSettings.setText("Settings");
        jbSettings.setToolTipText("Tweaks");
        jbSettings.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbSettings.setFocusPainted(false);
        jbSettings.setFocusable(false);
        jbSettings.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jbSettingsMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jbSettingsMouseEntered(evt);
            }
        });
        jbSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSettingsActionPerformed(evt);
            }
        });

        jbAbout.setBackground(new java.awt.Color(44, 44, 44));
        jbAbout.setForeground(new java.awt.Color(254, 254, 254));
        jbAbout.setText("About");
        jbAbout.setToolTipText("About Chocolate");
        jbAbout.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbAbout.setFocusPainted(false);
        jbAbout.setFocusable(false);
        jbAbout.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jbAboutMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jbAboutMouseEntered(evt);
            }
        });
        jbAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAboutActionPerformed(evt);
            }
        });

        jbExit.setBackground(new java.awt.Color(44, 44, 44));
        jbExit.setForeground(new java.awt.Color(254, 254, 254));
        jbExit.setText("Exit");
        jbExit.setToolTipText("Exit Chocolate");
        jbExit.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbExit.setFocusPainted(false);
        jbExit.setFocusable(false);
        jbExit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jbExitMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jbExitMouseEntered(evt);
            }
        });
        jbExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExitActionPerformed(evt);
            }
        });

        jbFormVisible.setBackground(new java.awt.Color(44, 44, 44));
        jbFormVisible.setForeground(new java.awt.Color(254, 254, 254));
        jbFormVisible.setText("Settings");
        jbFormVisible.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jbFormVisible.setFocusPainted(false);
        jbFormVisible.setFocusable(false);
        jbFormVisible.setVisible(false);
        jbFormVisible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbFormVisibleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainMenuPanelLayout = new javax.swing.GroupLayout(mainMenuPanel);
        mainMenuPanel.setLayout(mainMenuPanelLayout);
        mainMenuPanelLayout.setHorizontalGroup(
            mainMenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainMenuPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainMenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbAbout, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainMenuPanelLayout.createSequentialGroup()
                        .addComponent(jbStart, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbStop, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(mainMenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainMenuPanelLayout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jbExit, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainMenuPanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jbSettings, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(mainMenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainMenuPanelLayout.createSequentialGroup()
                    .addContainerGap(294, Short.MAX_VALUE)
                    .addComponent(jbFormVisible, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(10, 10, 10)))
        );
        mainMenuPanelLayout.setVerticalGroup(
            mainMenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainMenuPanelLayout.createSequentialGroup()
                .addContainerGap(289, Short.MAX_VALUE)
                .addGroup(mainMenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbStart)
                    .addComponent(jbSettings)
                    .addComponent(jbStop))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainMenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbExit)
                    .addComponent(jbAbout))
                .addContainerGap())
            .addGroup(mainMenuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainMenuPanelLayout.createSequentialGroup()
                    .addContainerGap(291, Short.MAX_VALUE)
                    .addComponent(jbFormVisible)
                    .addGap(37, 37, 37)))
        );

        mainPanel.add(mainMenuPanel, new java.awt.GridBagConstraints());

        jLSkullRed.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Chocolate/skullRed.jpg"))); // NOI18N
        jLSkullRed.setFocusable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        mainPanel.add(jLSkullRed, gridBagConstraints);

        jLSkullGreen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Chocolate/skullGreen.jpg"))); // NOI18N
        jLSkullGreen.setFocusable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        mainPanel.add(jLSkullGreen, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(startPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 350, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(startPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents
  
    private void jbAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAboutActionPerformed
        colourLooper = 2;
        new About().setVisible(true); //Display About Frame
        jLSkullRed.setEnabled(false);
        setVisible(false);
        setEnabled(false);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }//GEN-LAST:event_jbAboutActionPerformed

    private void jbSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSettingsActionPerformed
        colourLooper = 2;
        new Settings().setVisible(true); //Display Settings Frame
        jLSkullRed.setEnabled(false);
        setVisible(false);
        setEnabled(false);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }//GEN-LAST:event_jbSettingsActionPerformed

    private void jbStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbStartActionPerformed
        
        //jbStart.setVisible(false);
        jbStart.setEnabled(false);
        //jbStop.setVisible(true);
        //jbStop.setEnabled(true);
        //jbSettings.setEnabled(false);
        //jbExit.setEnabled(false);
        
        //Server Starter
        ProxyRunner.runServers();
                // Wait till all servers are started
			while (HTTPProxyServerStarter.started == 0
					|| SSLProxyStarter.started == 0
					|| HttpsServerStarter_443.started == 0
					|| HttpsServerStarter_8443.started == 0) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
				}

			}
			// Check if all servers are started successfully.
			if (HTTPProxyServerStarter.started == 2
					|| SSLProxyStarter.started == 2
					|| HttpsServerStarter_443.started == 2
					|| HttpsServerStarter_8443.started == 2) {
				final String currentDir = new File("").getAbsolutePath();

                                
                                
                                JOptionPane.showMessageDialog(null,
						"Proxy Not Started.( Probably Port is in use ) Pls. refer the log file in : "
								+ currentDir + File.separator + "ProxyLog.txt",
						"Starting Failed", JOptionPane.ERROR_MESSAGE);
                                
			//	jbStop.doClick();
                                
			}
                        if (HTTPProxyServerStarter.started == 0
					|| SSLProxyStarter.started == 0
					|| HttpsServerStarter_443.started == 0
					|| HttpsServerStarter_8443.started == 0) {
                            
                                    jLSkullRed.setVisible(false);
                                    jLSkullGreen.setVisible(true);
                        
                        final String currentDir = new File("").getAbsolutePath();
			JOptionPane.showMessageDialog(null,
					"Proxy Started.\r\nPls. refer the log file in : "
							+ currentDir + File.separator + "ProxyLog.txt",
					"Starting successfully", JOptionPane.INFORMATION_MESSAGE);
                        }
           (new TransferCount()).start(); //Start transfer count thread
           
    }//GEN-LAST:event_jbStartActionPerformed

    private void jbExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExitActionPerformed

        System.exit(0);
    }//GEN-LAST:event_jbExitActionPerformed

    private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped
        //Get keystrokes
        secretCode = secretCode + evt.getKeyChar();

            
            
        if (startPanel.isVisible() == true){
            if (secretCode.toLowerCase().contains("justine")) { //If Justine, unlock
                secretCode = "";
                startPanelLabel1.setIcon(new javax.swing.ImageIcon(getClass()
                        .getResource("/Chocolate/Animate.gif")));
                startTuneClip.setFramePosition(0);
                startTuneClip.start();
                (new RunCount()).start();

            }
            if (secretCode.toLowerCase().contains("exit")) {
                    System.exit(0);
            }
            if (startTuneClip.isActive() == true && startPanel.isVisible() == true){ //If x, skip intro
                if (secretCode.toLowerCase().contains("x")) {
                    secretCode = "";
                    startTuneClip.close();
                    mainPanel.setEnabled(true);
                    mainPanel.setVisible(true);
                    startPanel.setVisible(false);
                    startPanel.setEnabled(false);
                }
            }
        }
        
        //Keystrokes for start,about,exit and settings while the main menu is visible
        if (startPanel.isVisible() == false) {
            if (secretCode.toLowerCase().contains("start") && jbStart.isVisible() == true) {
                secretCode = "";
                jbStart.doClick();
            }
            if (secretCode.toLowerCase().contains("about") && jbExit.isEnabled() == true) {
                secretCode = "";
                jbAbout.doClick();
            }
            if (secretCode.toLowerCase().contains("exit") && jbExit.isEnabled() == true) {
                System.exit(0);
            }
            if (secretCode.toLowerCase().contains("settings") && jbStart.isVisible() == true) {
                    jbSettings.doClick();
                    
            }
        }
    }//GEN-LAST:event_formKeyTyped

    private void jbFormVisibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbFormVisibleActionPerformed
        //Get window coordinates
        if( Settings.x > 0 && Settings.y > 0){
            setLocation(Settings.x, Settings.y);
            Settings.x = 0;
            Settings.y = 0;
        }
        if( About.x > 0 && About.y > 0){
            setLocation(About.x, About.y);
            About.x = 0;
            About.y = 0;
        }
        setEnabled(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        jLSkullRed.setEnabled(true);
        
        setVisible(true);
    }//GEN-LAST:event_jbFormVisibleActionPerformed

    private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved
      // setLocation(x,y);
        x = getX();
        y = getY();
    }//GEN-LAST:event_formComponentMoved

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown


    }//GEN-LAST:event_formComponentShown

    private void jbStartMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbStartMouseEntered
        colourLooper = 1;
        whichButton = jbStart.getText();
        (new Looper()).start();
    }//GEN-LAST:event_jbStartMouseEntered

    private void jbStartMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbStartMouseExited
        colourLooper = 2;
       try {
            Thread.sleep(10);
            jbStart.setBackground(new java.awt.Color(44,44,44));
        } 
       catch (InterruptedException ex) {
            Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbStartMouseExited

    private void jbSettingsMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbSettingsMouseEntered
        if (jbSettings.isEnabled()==true) {
            colourLooper = 1;
            whichButton = jbSettings.getText();
            (new Looper()).start();
        }
    }//GEN-LAST:event_jbSettingsMouseEntered

    private void jbSettingsMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbSettingsMouseExited
        colourLooper = 2;
        try {
            Thread.sleep(10);
            jbSettings.setBackground(new java.awt.Color(44,44,44));
        } 
        catch (InterruptedException ex) {
            Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbSettingsMouseExited

    private void jbAboutMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbAboutMouseEntered
        colourLooper = 1;
        whichButton = jbAbout.getText();
        (new Looper()).start();
    }//GEN-LAST:event_jbAboutMouseEntered

    private void jbAboutMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbAboutMouseExited
        colourLooper = 2;
        try {
            Thread.sleep(10);
            jbAbout.setBackground(new java.awt.Color(44,44,44));
        } 
        catch (InterruptedException ex) {
            Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbAboutMouseExited

    private void jbExitMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbExitMouseEntered
        if (jbExit.isEnabled() == true) {
            colourLooper = 1;
            whichButton = jbExit.getText();
            (new Looper()).start();
        }
    }//GEN-LAST:event_jbExitMouseEntered

    private void jbExitMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbExitMouseExited
        colourLooper = 2;
        try {
            Thread.sleep(10);
            jbExit.setBackground(new java.awt.Color(44,44,44));
        } 
        catch (InterruptedException ex) {
            Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbExitMouseExited

    private void jbStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbStopActionPerformed

    /*    HTTPProxyServerStarter.started = 2;
        SSLProxyStarter.started = 2;
        HttpsServerStarter_443.started = 2;
        HttpsServerStarter_8443.started = 2;

        jbStop.setVisible(false);
        jbStop.setEnabled(false);
        jbStart.setVisible(true);
        jbStart.setEnabled(true);
        jbSettings.setEnabled(true);
        jbExit.setEnabled(true);

        jLSkullRed.setVisible(true);
        jLSkullGreen.setVisible(false);
        ProxyConstants.TOTAL_TRANSFER = 0;  */
    }//GEN-LAST:event_jbStopActionPerformed

    private void jbStopMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbStopMouseEntered
        /*   colourLooper = 1;
        whichButton = jbStop.getText();
        (new Looper()).start();*/
    }//GEN-LAST:event_jbStopMouseEntered

    private void jbStopMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbStopMouseExited
        /*  colourLooper = 2;

        try {
            Thread.sleep(10);

            jbStop.setBackground(new java.awt.Color(44,44,44));
        } catch (InterruptedException ex) {
            Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
        }*/
    }//GEN-LAST:event_jbStopMouseExited

    /**
     * @param args the command line arguments
     */
        
    public static void main(String args[]) {
        final MainUI proxyUIStarter = new MainUI();
	final InputStream is = proxyUIStarter.getClass().getResourceAsStream(
				"/ProxyCertificate.ser");
	proxyUIStarter.copyFile(proxyUIStarter, is);

	final String currentDir = new File("").getAbsolutePath();
	final String cerfile = currentDir + File.separator
				+ "ProxyCertificate.ser";

	try {
            final PrintStream stdout = new PrintStream(new FileOutputStream(
					ProxyConstants.LOG_FILE));
            System.setOut(stdout);
        }
        catch (final FileNotFoundException e) {
            final String currentDir1 = new File("").getAbsolutePath();
            JOptionPane.showMessageDialog(null,
            "Could not access LOG file in  : " + currentDir1
            + File.separator + "ProxyLog.txt", "Message",
            JOptionPane.ERROR_MESSAGE);
	}
        
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
           
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainUI().setVisible(true);
            }
        });
    }
    
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JLabel jLSkullGreen;
    public static javax.swing.JLabel jLSkullRed;
    public static javax.swing.JButton jbAbout;
    public static javax.swing.JButton jbExit;
    public static javax.swing.JButton jbFormVisible;
    public static javax.swing.JButton jbSettings;
    public static javax.swing.JButton jbStart;
    public static javax.swing.JButton jbStop;
    public static javax.swing.JPanel mainMenuPanel;
    public static javax.swing.JPanel mainPanel;
    public static javax.swing.JPanel startPanel;
    public static javax.swing.JLabel startPanelLabel1;
    // End of variables declaration//GEN-END:variables
}
